package com.sci99.ex.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import com.sci99.ex.model.Department;
import com.sci99.ex.util.MyBatisUtil;

public class DptService {
	static Logger log = Logger.getLogger(DptService.class);

	public static List<Department> getDepartment() throws Exception {
		SqlSession s = MyBatisUtil.getMsSqlSession();
		List<Department> l = s.selectList("getDepartment");
		s.close();
		return l;
	}

	public static List<Department> getDepartment(String name) throws Exception {
		SqlSession s = MyBatisUtil.getMsSqlSession();
		Map<String, Object> args = new HashMap<>(2, 1);
		args.put("dptName", name);
		List<Department> l = s.selectList("getDepartment", args);
		s.close();
		return l;
	}

	public static List<Department> getDepartmentWithout(List<Department> dps)
			throws Exception {
		return getDepartmentWithout(dps, null);
	}

	public static List<Department> getDepartmentWithout(List<Department> dps,
			String name) throws Exception {
		SqlSession s = MyBatisUtil.getMsSqlSession();
		Map<String, Object> args = new HashMap<>();
		args.put("list", dps);
		args.put("dptName", name);
		List<Department> l = s.selectList("getDepartmentWithout", args);
		s.close();
		return l;
	}

	/**
	 * 按课程取被分配的部门
	 * 
	 * @param cid
	 * @return
	 * @throws Exception
	 */
	public static List<Department> getDptListByCouId(Long cid) throws Exception {
		SqlSession mysql = MyBatisUtil.getSession();
		SqlSession sqlserver = MyBatisUtil.getMsSqlSession();
		Map<String, Object> args = new HashMap<>(2, 1);
		args.put("cid", cid);
		List<Long> ids = mysql.selectList("getDepIdsByCid", args);
		if (ids.size() == 0)
			return null;
		Map<String, Object> args2 = new HashMap<>(2, 1);
		args2.put("ids", ids);
		List<Department> rs = sqlserver.selectList("getDepListByCid", ids);
		mysql.close();
		sqlserver.close();
		return rs;
	}
}
